package forrest.triv;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class list extends ListActivity {
	String[] names;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		buildui();
	}
	public void buildui(){
		try {
			URL url=new URL("http://the-extendible-trivia-application.googlecode.com/files/urlslist.txt");
			HttpURLConnection con=(HttpURLConnection) url.openConnection();
			InputStream in=con.getInputStream();
			byte[] bytes=new byte[con.getContentLength()];
			in.read(bytes);
			ArrayList<String> str=new ArrayList<String>();
			int readsize;
			str.add(new String(bytes));
			in.close();
			String urls=new String();
			for(int j=0;j<str.size();j++){
				urls+=str.get(j);
			}
			String[] strings;
			String temp="";
			int i=0;
			if(urls.contains("\r\n")){
				strings=urls.split("\r\n");
			}
			else{
				strings=urls.split("\n");
			}
			names=new String[strings.length];
			names=strings;
			String[] words=new String[names.length];
			for(int ind=0;ind<names.length;ind++){
				words[ind]=names[ind];//.substring(56);
			}
			makeui(names);
		}
		catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			Toast toast=Toast.makeText(this, "ERROR: server not found, please check your internet settings", Toast.LENGTH_SHORT);
			toast.show();
			e.printStackTrace();
		}
	}
	public void makeui(String[] words){
		this.setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,words));
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		try {
			URL url=new URL(names[position]);
			InputStream in=url.openStream();
			byte[] bytes=new byte[1024*5];
			int readsize=in.read(bytes);
			in.close();
			write(bytes);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			
			e.printStackTrace();
		}
		super.onListItemClick(l, v, position, id);
	}
	
	public void write(byte[] bytes){
		String str=new String(bytes);
	//	Toast toast=Toast.makeText(this, str, Toast.LENGTH_LONG);
	//	toast.show();
		BufferedWriter write=null;
		try {
			write=new BufferedWriter(new FileWriter(Environment.getExternalStorageDirectory()+"/TETA/questions.txt",true));
			write.write(str);
			write.flush();
			write.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//Toast toast=Toast.makeText(this, "SUCSESS!,restart for changes to take effect", Toast.LENGTH_SHORT);
		//toast.show();
		
	}
	
}